package com.leaf.collegeidleapp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.leaf.collegeidleapp.adapter.MyCollectionAdapter;
import com.leaf.collegeidleapp.bean.Collection;
import com.leaf.collegeidleapp.bean.Commodity;
import com.leaf.collegeidleapp.toucheventtest.SideslipListView;
import com.leaf.collegeidleapp.util.MyCollectionDbHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyCollectionActivity_2 extends AppCompatActivity {

    private static final String TAG = "MyCollectionActivity_2";
    private SideslipListView mSideslipListView;
    TextView tvStuId;
    List<Collection> myCollections = new ArrayList<>();
    MyCollectionDbHelper dbHelper;
    CustomAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection_2);

        //返回
        ImageButton ibBack = findViewById(R.id.ib_back);
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tvStuId = findViewById(R.id.tv_stuId);
        tvStuId.setText(this.getIntent().getStringExtra("stuId"));
        mSideslipListView = (SideslipListView) findViewById(R.id.sideslipListView);
        dbHelper = new MyCollectionDbHelper(getApplicationContext(),MyCollectionDbHelper.DB_NAME,null,1);
        myCollections = dbHelper.readMyCollections(tvStuId.getText().toString());
        adapter = new CustomAdapter(getApplicationContext());
        adapter.setData(myCollections);
        mSideslipListView.setAdapter(adapter);//设置适配器

        //设置item点击事件
        mSideslipListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mSideslipListView.isAllowItemClick()) {
                    Collection collection = (Collection)  mSideslipListView.getAdapter().getItem(position);
                    Bundle bundle1 = new Bundle();
                    bundle1.putInt("position",position);
                    bundle1.putByteArray("picture",collection.getPicture());
                    bundle1.putString("title",collection.getTitle());
                    bundle1.putString("description",collection.getDescription());
                    bundle1.putFloat("price",collection.getPrice());
                    bundle1.putString("phone",collection.getPhone());
                    bundle1.putString("stuId",tvStuId.getText().toString());

                    Intent intent = new Intent(MyCollectionActivity_2.this, MyCollectionActivity_detail.class);
                    intent.putExtras(bundle1);
                    startActivity(intent);
                    Log.i(TAG, myCollections.get(position) + "被点击了");
                    Toast.makeText(MyCollectionActivity_2.this, myCollections.get(position) + "被点击了",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        //设置item长按事件
        mSideslipListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (mSideslipListView.isAllowItemClick()) {
                    Log.i(TAG, myCollections.get(position) + "被长按了");
                    Toast.makeText(MyCollectionActivity_2.this, myCollections.get(position) + "被长按了",
                            Toast.LENGTH_SHORT).show();
                    return true;//返回true表示本次事件被消耗了，若返回
                }
                return false;
            }
        });
    }

    /**
     * 自定义ListView适配器
     */
    class CustomAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater layoutInflater;
        private List<Collection> collections = new ArrayList<>();
        HashMap<Integer,View> location = new HashMap<>();

        public CustomAdapter(Context context) {
            this.context = context;
            layoutInflater = LayoutInflater.from(context);
        }

        public void setData(List<Collection> collections) {
            this.collections = collections;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return collections.size();
        }

        @Override
        public Object getItem(int position) {
            return collections.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (null == convertView) {
                convertView = View.inflate(MyCollectionActivity_2.this, R.layout.layout_my_collection2, null);
                Collection collection = (Collection) getItem(position);
                viewHolder = new ViewHolder(convertView,collection);
                //viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
                viewHolder.txtv_delete = (TextView) convertView.findViewById(R.id.txtv_delete);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //viewHolder.textView.setText(mDataList.get(position));
            final int pos = position;
            viewHolder.txtv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //根据商品名称,商品描述和价格执行删除操作
                    Collection collection = (Collection) adapter.getItem(pos);
                    //删除收藏商品项
                    dbHelper.deleteMyCollection(collection.getTitle(),collection.getDescription(),collection.getPrice());

                    Toast.makeText(MyCollectionActivity_2.this, collections.get(pos) + "被删除了",
                            Toast.LENGTH_SHORT).show();
                    collections.remove(pos);
                    notifyDataSetChanged();
                    mSideslipListView.turnNormal();
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        //public TextView textView;
        public TextView txtv_delete;
        ImageView ivCommodity;
        TextView tvTitle,tvDescription,tvPrice,tvPhone;

        public ViewHolder(View itemView, Collection collection) {
            tvTitle = itemView.findViewById(R.id.tv_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvDescription = itemView.findViewById(R.id.tv_description);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            ivCommodity = itemView.findViewById(R.id.iv_commodity);
            tvTitle.setText(collection.getTitle());
            tvDescription.setText(collection.getDescription());
            tvPrice.setText(String.valueOf(collection.getPrice())+"元");
            tvPhone.setText(collection.getPhone());
            byte[] picture = collection.getPicture();
            Bitmap img = BitmapFactory.decodeByteArray(picture,0,picture.length);
            ivCommodity.setImageBitmap(img);
        }

    }
}